package com.miplus.ccrm.uploadjob.service.xsky;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
import com.miplus.ccrm.common.JPubUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

@Slf4j
@Service
public class XSkyService {
    @Value("${xsky.aws.bucketName}")
    private String aws_bucketName;

    private final AmazonS3 s3Client;

    @Autowired
    public XSkyService(AmazonS3 s3)
    {
        log.info("XSkyService.build:{}",s3);
        this.s3Client=s3;
    }

    public void OnPlayRecord(String skyid, HttpServletResponse response) {
        try {
            log.info("OnPlayRecord({}):{}",skyid,aws_bucketName);

            if(JPubUtils.IsEmpty(skyid)) return;

            // 调用OSS服务下载文件,下载文件方式
            // s3Client.getObject(new GetObjectRequest(aws_bucketName, skyid), new File(localPath));
            S3Object fullObject=s3Client.getObject(aws_bucketName, skyid);
            byte[] bytes =fullObject.getRedirectLocation().getBytes();

            response.addHeader("Content-Type","audio/mpeg;charset=utf-8");
            response.addHeader("Content-Length",bytes.length+"");
            ServletOutputStream out=response.getOutputStream();
            out.write(bytes,0,bytes.length);
            out.flush();
            out.close();

            log.info("OnPlayRecord({}):Ok",skyid);

        } catch (Exception e) {
            log.error("OnPlayRecord.Err:",e);
        }
    }

    public void OnPlayFile(HttpServletResponse response)
    {
        String fwav="d:/java/ringing.wav";
        log.info("OnPlayFile:{}",fwav);
        response.addHeader("Content-Type","audio/mpeg;charset=utf-8");
        try{
            File file=new File(fwav);
            byte[] buf=new byte[1024];
            FileInputStream fis=new FileInputStream(file);
            ServletOutputStream out=response.getOutputStream();
            int len_l=fis.read(buf);
            while (len_l!=-1){
                out.write(buf,0,len_l);
                len_l=fis.read(buf);
            }
            out.flush();
            out.close();
            fis.close();
        }
        catch (Exception e){
            log.error("OnPlayFile.Err:",e);
        }
    }

    private boolean fileExists(String keyid) {
        return s3Client.doesObjectExist(aws_bucketName, keyid);
    }

    public void DownloadFile(String skyid, HttpServletResponse response) {
        try {
            log.info("DownloadFile({}):{}",skyid,aws_bucketName);

            if(JPubUtils.IsEmpty(skyid)) return;

            // 调用OSS服务下载文件,下载文件方式
            // s3Client.getObject(new GetObjectRequest(aws_bucketName, skyid), new File(localPath));

            response.addHeader("Content-Type","application/octet-stream;charset=utf-8");
            response.addHeader("Content-Disposition","attachment; filename="+skyid);
            S3Object fullObject=s3Client.getObject(aws_bucketName, skyid);
            byte[] bytes =fullObject.getRedirectLocation().getBytes();
            ServletOutputStream out=response.getOutputStream();
            out.write(bytes,0,bytes.length);
            out.flush();
            out.close();

            log.info("DownloadFile({}):Ok",skyid);

        } catch (Exception e) {
            log.error("DownloadFile.Err:",e);
        }
    }

    public boolean UploadFile(String keyid, String localPath) {
        try {
            log.info("UploadFile({}) From {}",keyid,localPath);
            File file = new File(localPath);
            PutObjectResult result=s3Client.putObject(aws_bucketName, keyid, file);

            log.info("UploadFile({}) OK:{}",keyid,result);

            return true;
        } catch (Exception e) {
            log.error("UploadFile.Err:",e);
            return false;
        }
    }

    public boolean UploadFile(String keyid, File file) {

        log.info("UploadFile({}) From file",keyid);

        PutObjectResult result=s3Client.putObject(aws_bucketName, keyid, file);

        log.info("UploadFile({}) OK:{}",keyid,result);

        return true;
    }
}
